import { _decorator, Component, Sprite, SpriteAtlas, SpriteFrame, UITransform } from 'cc';
import { DataMgr } from '../../Basic/Manager/DataManager';
import { Tool } from '../../Util/Tool';
import { GameModel } from './GameModel';
const { ccclass, property } = _decorator;

@ccclass('ColorItem')
export class ColorItem extends Component {
  @property(Sprite)
  sp: Sprite;
  @property(UITransform)
  uiTrans: UITransform;
  @property(Sprite)
  icon: Sprite;
  set Width(w) {
    this.uiTrans.width = w;
  }
  get Width() {
    return this.uiTrans.width;
  }

  set Height(h) {
    this.uiTrans.height = h;
  }
  get Height() {
    return this.uiTrans.height;
  }
  curColor: string;
  /** 销毁标记 */
  destroyFlag: boolean = false;
  parentCol: number;
  parentRow: number;

  setColor(color: string, col: number, row: number) {
    this.curColor = color;
    this.parentCol = col;
    this.parentRow = row;
    if (color != `-1`) {
      const colorData = DataMgr.tables.TbColor.get(color);
      const imgData = colorData.cakeImg != `` ? DataMgr.tables.TbImg.get(colorData.cakeImg) : null;
      this.sp.spriteFrame = imgData != null ? this.sp.spriteAtlas.getSpriteFrame(imgData.imgName) : null;
      if (imgData != null || this.sp.spriteFrame === undefined) {
        this.sp.spriteFrame = this.sp.spriteAtlas.getSpriteFrame(imgData.imgName);
        // if (imgData.altasPath === '') {
        //   GameModel.Ins()
        //     .loadDynamicRes<SpriteFrame>(imgData.imgName + '/spriteFrame')
        //     .then((spr) => {
        //       this.sp.spriteFrame = spr;
        //     });
        // } else {
        //   GameModel.Ins()
        //     .loadDynamicRes<SpriteAtlas>(imgData.altasPath)
        //     .then((altas) => {
        //       this.sp.spriteFrame = altas.getSpriteFrame(imgData.imgName);
        //     });
        // }
        // Tool.getSpriteByKey(imgData.altasPath, imgData.imgName).then((sp) => {
        //   this.sp.spriteFrame = sp;
        // });
      }

      const iconImg = colorData.fruitImg != `` ? DataMgr.tables.TbImg.get(colorData.fruitImg) : null;
      this.icon.spriteFrame = iconImg != null ? this.sp.spriteAtlas.getSpriteFrame(iconImg.imgName) : null;
      if (iconImg != null || this.icon.spriteFrame === undefined) {
        // Tool.getSpriteByKey(iconImg.altasPath, iconImg.imgName).then((sp) => {
        //   this.icon.spriteFrame = sp;
        // });
        this.icon.spriteFrame = this.sp.spriteAtlas.getSpriteFrame(iconImg.imgName);
        // if (iconImg.altasPath === '') {
        //   GameModel.Ins()
        //     .loadDynamicRes<SpriteFrame>(iconImg.imgName + '/spriteFrame')
        //     .then((spr) => {
        //       this.icon.spriteFrame = spr;
        //     });
        // } else {
        //   GameModel.Ins()
        //     .loadDynamicRes<SpriteAtlas>(iconImg.altasPath)
        //     .then((altas) => {
        //       this.icon.spriteFrame = altas.getSpriteFrame(iconImg.imgName);
        //     });
        // }
      }
    } else {
      this.sp.spriteFrame = null;
      this.icon.spriteFrame = null;
    }
  }

  reset() {
    this.curColor = '';
    this.destroyFlag = false;
    this.sp.spriteFrame = null;
    this.icon.spriteFrame = null;
    this.Width = 56;
    this.Height = 56;
    this.parentCol = -1;
    this.parentRow = -1;
  }
}
